package chloroform.oauth

import java.io.{InputStreamReader, BufferedReader, OutputStreamWriter}
import java.lang.String
import java.net.{URLDecoder, HttpURLConnection, URL}

/**
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: 1.3.11
 * Time: 14:13
 * To change this template use File | Settings | File Templates.
 */

@deprecated
object UrlHttpRequestExecutor {
  def createUrlFromRequest(request: HttpRequest): URL = {
    new URL(if (request.ssl) "https" else "http", request.host, request.path)
  }
}

@deprecated
trait UrlHttpRequestExecutor extends HttpRequestExecutor {

  def execute(request: HttpRequest) = {
    val url = UrlHttpRequestExecutor.createUrlFromRequest(request)

    val conn = url.openConnection.asInstanceOf[HttpURLConnection]
    conn.setRequestMethod(request.method)
    request.headers.foreach(header => conn.setRequestProperty(header._1, header._2))

    val response = if (request.method == "POST") {
//      conn.setDoOutput(true)
//      val wr = new OutputStreamWriter(conn.getOutputStream())
//      wr.write(request.body)
//      wr.flush()
//
//      if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
//        // OK
//      } else {
//        // Server returned HTTP error code.
//      }
//
      // todo
      null
    } else {

      new HttpResponse {
        val body = conn.getInputStream

        def header(name: String) = conn.getHeaderField(name)

        def responseCode() = conn.getResponseCode
      }
    }

    response
  }
}